#include <my_header.h>
#if 0
书写程序，创建3个子线程，然后等待所有的子线程终止，
最后还要获取其退出状态。
#endif
/* Usage: ./5_捕获状态 */
// 线程1处理函数
void *threadFunc1(void *arg)
{
    printf("threadFunc1 over\n");
    // 手打取消点
    // pthread_testcancel();
    return (void *)1;
}
// 线程2处理函数
void *threadFunc2(void *arg)
{
    printf("threadFunc2 over\n");
    // pthread_testcancel();
    return (void *)2;
}
// 线程3处理函数
void *threadFunc3(void *arg)
{

    printf("threadFunc3 over\n");
    // pthread_testcancel();
    return (void *)3;
}


int main(void){                                  

    pthread_t tid1, tid2, tid3;
    // 创建线程1
    int ret1 = pthread_create(&tid1, NULL,&threadFunc1, NULL);
    THREAD_ERROR_CHECK(ret1, "pthread_create");
    // 创建线程2
    int ret2 = pthread_create(&tid2, NULL,&threadFunc2, NULL);
    THREAD_ERROR_CHECK(ret2, "pthread_create");
    // 创建线程3
    int retr3 = pthread_create(&tid3, NULL,&threadFunc3, NULL);
    THREAD_ERROR_CHECK(retr3, "pthread_create");
    
    // pthread_cancel() 取消一个执行中的线程
    // 线程1主动退出
    int ret_cancel1 = pthread_cancel(tid1);
    THREAD_ERROR_CHECK(ret_cancel1, "pthread_cancel");
     // 线程2主动退出
    int ret_cancel2 = pthread_cancel(tid2);
    THREAD_ERROR_CHECK(ret_cancel2, "pthread_cancel");
    // 线程1主动退出
    int ret_cancel3 = pthread_cancel(tid3);
    THREAD_ERROR_CHECK(ret_cancel3, "pthread_cancel");

    void *retThread1, *retThread2, *retThread3;
    // 捕获线程1状态
    int ret_join1 = pthread_join(tid1, &retThread1);
    THREAD_ERROR_CHECK(ret_join1,"pthread_join");
    printf("child thread rerval = %ld", (long) retThread1);
    // 捕获线程2状态
    int ret_join2 = pthread_join(tid2, &retThread2);
    THREAD_ERROR_CHECK(ret_join2,"pthread_join");
    printf("child thread rerval = %ld", (long) retThread2);
    // 捕获线程三状态
    int ret_join3 = pthread_join(tid3, &retThread3);
    THREAD_ERROR_CHECK(ret_join3,"pthread_join");
    printf("child thread rerval = %ld", (long) retThread3);
    
    return 0;
}


